#!/usr/bin/env bash
# Copyright (C) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See LICENSE in project root for information.

# publish [dirname | --top]
# Copies the built documentation in $BUILD_ARTIFACTS/docs to the docs container
# at the specified directory name which defaults to $MML_VERSION; use `--top` to
# copy it to the toplevel so it's the default docs content.

. "$(dirname "${BASH_SOURCE[0]}")/../../runme" "$@"
main() (

target="$1"; shift
if [[ -z "$target" ]]; then target="$MML_VERSION"; fi
if [[ "x$target" = "x--top" ]]; then
  show section "Publishing docs to toplevel"; target=""
else
  show section "Publishing docs to $target"
fi

default="index.html"
mime_types=(
  # note: glob patterns are matched against the whole copied path, so they
  # should always start with a `*`
  "*.html;text/html"
  "*.png;image/png"
  "*.gif;image/gif"
  "*.svg;image/svg+xml"
  "*.js;application/javascript"
  "*.css;text/css"
  "*.ttf;application/font-sfnt"
  "*.woff;font/woff"
  "*.woff2;font/woff2"
  "*.eot;application/vnd.ms-fontobject"
  "*.txt;text/plain"
  "*.doctree;application/octet-stream"
  "*/.buildinfo;application/octet-stream"
  "*/objects.inv;application/octet-stream"
  "*/environment.pickle;application/octet-stream"
)

cd "$BUILD_ARTIFACTS/docs"
restore=$(shopt -p dotglob); shopt -s dotglob; files=( ** ); $restore

# Make $default files when missing
fst=1
for f in . "${files[@]}"; do
  x="$f/$default"
  if [[ ! -d "$f" || -e "$x" ]]; then continue; fi
  if ((fst)); then show - "creating default $default files"; fst=0; fi
  show - "  $x"
  { echo "<html><body><pre style=\"font-size: 150%;\">"
    for y in "$f"/*; do
      u="$(basename "$y")"; if [[ -d "$y" ]]; then u+="/"; fi
      if [[ "$u" = "$default" ]]; then continue; fi
      echo "<a href=\"$u\">$u</u>"
    done
    echo "</pre></body></html>"
  } > "$x"
  files+=("$x")
done

len=${#files[@]}

# copy all files with a proper type
for mt in "${mime_types[@]}"; do
  glob="${mt%;*}"; type="${mt##*;}"
  show - "Copying $glob as $type"
  for ((i=0; i<len; i++)); do
    # mimics the matching that `upload-batch` with `--pattern` is doing
    if [[ -n "${files[$i]}" && "${files[$i]}" = $glob ]]; then unset -v "files[$i]"; fi
  done
  collect_log=1 _ azblob upload-batch \
    --source "." --destination "$DOCS_CONTAINER${target:+/}$target" \
    --pattern "$glob" --content-type "$type"
done

# Deal with directories
show - "Making directory defaults"
tmp="/tmp/mmlbuild-$$"
if [[ -z "$target" ]]; then start=0; else start=-1; fi
for ((i=-1; i<len; i++)); do
  if ((i<0)) # -1 for the top directory (can't do this for toplevel target)
  then f="."; webdir="$MML_VERSION"; redirect="$MML_VERSION/"
  else f="${files[$i]}"; webdir="$target${target:+/}$f"; redirect="${f##*/}/"; fi
  if [[ -z "$f" || ! -d "$f" ]]; then continue; fi
  unset -v "files[$i]"
  if [[ ! -e "$f/$default" ]]; then continue; fi
  # copy "foo/$default" also to "foo/"
  collect_log=1 _ azblob upload \
    --container "$DOCS_CONTAINER" --content-type "text/html" \
    --file "$f/$default" --name "$webdir/" \
    2> /dev/null
  # create "foo" for redirections to "foo/"
  show command "... html redirect to $redirect ... > $(qstr "$tmp")"
  { echo "<html><head>"
    echo "<meta http-equiv=\"refresh\" content=\"0; url=$redirect\" />"
    echo "</head><body>"
    echo "Moved <a href=\"$redirect\">here</a>"
    echo "</body></html>"
  } > "$tmp"
  collect_log=1 _ azblob upload \
    --container "$DOCS_CONTAINER" --content-type "text/html" \
    --file "$tmp" --name "$webdir" \
    2> /dev/null
done
rm -f "$tmp"

if ((${#files[@]} > 0)); then
  echo "Leftovers: ${#files[@]}"
  printf '  %s\n' "${files[@]}"
  failwith "the above paths were not in any known patterns"
fi

)
__ main "$@"
